package com.imooc.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author songjun
 * @date 2019/7/24.
 */
@Aspect
@Component
@Slf4j
public class HttpAspect {

    @Pointcut("execution(public * com.imooc.controller.GirlController.*(..))")
    public void log() {
    }

    @Before("log()")
    public void doBefore(JoinPoint joinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest httpServletRequest = attributes.getRequest();
        StringBuffer requestURL = httpServletRequest.getRequestURL();
        log.info("requestURL={}", requestURL);
        String method = httpServletRequest.getMethod();
        log.info("method={}", method);
        String remoteUser = httpServletRequest.getRemoteAddr();
        log.info("ip={}", remoteUser);
        String typeName = joinPoint.getSignature().getDeclaringTypeName();
        String methodName = joinPoint.getSignature().getName();
        log.info("methodName={}", typeName + "." + methodName);

    }

    @After("log()")
    public void doAfter() {
        log.info("doAfter 222222");
    }

    @AfterReturning(pointcut = "log()",returning = "object")
    public void doAfterReturning(Object object) {
        log.info("response-{}",object);
    }

}
